package com.runyun.project.service.impl;

import com.runyun.project.entity.User;
import com.runyun.project.mapper.EmilMapper;
import com.runyun.project.mapper.UserMapper;
import com.runyun.project.service.EmilService;
import com.runyun.project.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
* @ClassName UserServiceImpl
* @Description (User)表服务实现类
**/
@Service
public class EmilServiceImpl implements EmilService {

    @Resource
    protected EmilService emilService;
    @Resource
    private EmilMapper emilMapper;

    //引入Spring mail依赖之后，会自动装配到IOC容器中
    @Autowired(required = false)
    private JavaMailSender sender;
    // 随机生成6位验证码的函数
    private String achieveCode() {
        String[] beforeShuffle= new String[] { "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F",
                "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a",
                "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
                "w", "x", "y", "z" };
        List list = Arrays.asList(beforeShuffle);
        Collections.shuffle(list);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i));
        }
        String afterShuffle = sb.toString();
        String result = afterShuffle.substring(3, 9);
        System.out.print(result);
        return result;
    }
    @Override
    public void sendCodeToEmailAddress(String email) {
        String code = achieveCode();//生成验证码
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject("背头云盘有用户添加您作为好友");//发送邮件的标题
        message.setText("我要添加你作为我独一无二的好友，快来跟我嘿嘿嘿");
        message.setTo(email);//收件人
        message.setFrom("1293076287@qq.com");//寄件人
        sender.send(message);
        /*
        * 将发送信息添加到邮箱表
        * */
        emilMapper.insertOne(email,code);

    }
}